﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using YakShaver.YakServer;
using YakShaver.Contract;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;

namespace YakShaver.CodePlex
{
    [Serializable]
    public class CodePlexWorkItemService :  IWorkItemProviderService
    {

        private TeamFoundationServer _teamFoundationServer = null;
        private ICredentialsProvider _credentials = null;
        private string _codePlexServerName = string.Empty;
        
        public CodePlexWorkItemService()
        {

        }

        public string CodePlexServerName
        {
            get 
            {
                if (_codePlexServerName == string.Empty)
                    _codePlexServerName = @"https://tfs06.codeplex.com";

                return _codePlexServerName;
            }
            set { _codePlexServerName = value; }
        }

        public ICredentialsProvider Credentials
        {
            get { return _credentials; }
            set { _credentials = value; }
        }
        
        #region IWorkItemProviderService Members

        public event EventHandler<WorkItemModifiedEventArgs> WorkItemModifedEvent;

        public void CreateWorkItem(YakShaverWorkItem workItem)
        {
            // thanks to Justin Long's blog to get me started.
            //http://dukk.org/blogs/justins_development_adventures/archive/2007/05/11/getting-started-with-the-team-foundation-server-api.aspx

            CodePlexProjectInfoSvc.ProjectInfoServiceSoapClient infoService = new YakShaver.CodePlex.CodePlexProjectInfoSvc.ProjectInfoServiceSoapClient();


                _teamFoundationServer = TeamFoundationServerFactory.GetServer(CodePlexServerName, new CodePlexCredentialsProvider());

                try
                {
                    _teamFoundationServer.Authenticate();
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine("exception: " + ex.Message);
                }
            WorkItemStore workItemStore = new WorkItemStore(_teamFoundationServer);
            
            
            foreach (Project project in workItemStore.Projects)
            {
                System.Diagnostics.Debug.WriteLine("Project Name: " + project.Name);

                foreach (Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemType workItemType in project.WorkItemTypes)
                {
                    System.Diagnostics.Debug.WriteLine("Work item type: " + workItemType.Name);

                }

                foreach (Node node in project.AreaRootNodes)
                {
                    System.Diagnostics.Debug.WriteLine("Node: " + node.Name + " Path: " + node.Path);
                }
                         
            }


        }

        public void ModifyWorkItem(YakShaverWorkItem workItem, IWorkItemDataContract dataContact)
        {
            if (this.WorkItemModifedEvent != null)
            {
                WorkItemModifedEvent(null, new WorkItemModifiedEventArgs(workItem.InstanceId, dataContact));
            }
        }

        
        //public void ModifyWorkItem(Guid instanceId, IWorkItemDataContract dataContract)
        //{
        //    WorkItemModifiedEventArgs args = new WorkItemModifiedEventArgs(instanceId,dataContract);
        //    EventHandler<WorkItemModifiedEventArgs> eventHandler = this.WorkItemModifedEvent;
        //    if (eventHandler != null)
        //    {
        //        eventHandler(null, args);
        //    }
          
        //}
        #endregion

        #region IWorkItemProviderService Members


      

        #endregion

        #region IWorkItemProviderService Members


        public void ModifyWorkItem(YakShaverWorkItem workItem)
        {
            throw new NotImplementedException();
        }

        #endregion
    } 
}
